<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
<link rel="stylesheet" href="{{ url('/common/mobile-common/common.css') }}">
<link rel="stylesheet" href="{{ url('/mobile/css/header.css') }}">		
<link rel="stylesheet" href="{{ url('/mobile/css/member/cart.css') }}">	
<script src="{{ url('mobile/js/jquery-1.8.3.min.js') }}"></script>
<link rel="stylesheet" href="{{ url('/') }}/layer/skin/layer.css" />
<script src="{{ url('/') }}/layer/layer.js"></script>
<script src="{{ url('/common/mobile-common/hmt.js') }}"></script>
</head>
<body>
	<div class="abouts">
		<div class="wrap-cont clearfix">
				<div class="wrap-cont wrap-cont2">
					<div class="all-check">
						<input type="checkbox" id="achecked">
						<label>全选</label>
						<span class="img1"><img src="{{ url('mobile/images/no-check.png') }}"></span>
						<em class="dele" data-id=""><img src="{{ url('mobile/images/sc-icon.png') }}"></em>
					</div>
					<div class="contt">
						<div class="goods">
						<?php $all = 0;?>
						@foreach($res as $v)
						<div class="goods-list">
							<input type="checkbox" class="goodsId" data-id="{{$v->id}}" name="checkone">
							<span class="img2"><img src="{{ url('mobile/images/no-check.png') }}"></span>
							<img class="good-img" src="{{ config('config.picHostPrefix').$v->small_img }}">
							<p class="good-tit">
								<span>{{$v->name}}</span>
								<em data-id="{{$v->id}}"><img src="{{ url('mobile/images/sc-icon.png') }}"></em>
							</p>
							<p class="good-type">
								<span>类型</span>
								<select>
									<option>{{$v->category}}</option>
								</select>
							</p>
							<p class="good-sl">
								<?php $memberVip = DB::table('members')->where('id',$memberId)->first();?>
								@if(!empty($v->vip_price)&&$memberVip->vip==1)
									@if($v->discount)
										<a>¥{{$v->vip_price*$v->discount}}</a><?php $all += $v->vip_price*$v->discount;?>
									@else
										<a class="pris">¥<em>{{$v->vip_price}}</em></a><?php $all += $v->vip_price;?>
									@endif
								@else
									@if($v->discount)
										<a>¥{{$v->price*$v->discount}}</a><?php $all += $v->price*$v->discount;?>
									@else
										<a class="pris">¥<em>{{$v->price}}</em></a><?php $all += $v->price;?>
									@endif
								@endif
								<i style="color: #999999">
									数量：<b style="font-weight:normal;">{{$v->num}}</b>
								</i>
							</p>
						</div>
						@endforeach
					</div>
					<div class="toSettle">
						<p>
							<span>合计: ¥<i>0</i>(不含运费)</span>
							<a href="javascript:void(0)" class="qjs nogoods">去结算<!--(<i>1</i>)--></a>
						</p>
					</div>
					</div>
					
				</div>
		</div>
	</div>
	<div class="noshop" style="display: none;text-align: center;margin-top: 200px;">购物车里暂无商品，去<a href="{{url('wap/')}}">首页</a></div>
</body>
<script>
	$(function(){
		//购物车内无商品时显示
		if($(".goods-list").length==0){
				$(".all-check,.toSettle").hide();
				$(".noshop").show();
			}
		//数量
		var IsNumber=true;
		function calcTotalPallets() {
		    var totalPallets = 0;
		    $("input[name=buy]").each(function() {
		        var thisValue = $(this).val();   
		        if ( (IsNumeric(thisValue)) &&  (thisValue != '') ) {       
		            totalPallets += parseInt(thisValue);       
		        };    
		    });   
		    $(".qjs i").val(totalPallets);
		};
		
		//全选
		$("#achecked").click(function(){			
			var allCheStatus=$(this).attr("checked");
			if(allCheStatus=='checked'){
				$(".toSettle a").removeClass("nogoods");
				$(".goods-list").find("input[type='checkbox']").attr("checked",true);
				$(".all-check label").text("取消全选");
				$(".img1").find("img").attr("src","{{ url('mobile/images/check.png') }}");
				$(".img2").find("img").attr("src","{{ url('mobile/images/check.png') }}");
				$(".all-check em").unbind().click(function(){
					var qdQs=confirm("确定删除商品吗？");
					if(qdQs==true){
						var id = new Array();
						$(".goods-list").each(function() {
							var idNum = $(this).find(".goodsId").attr("checked");
							if (idNum == 'checked') {
								var oid = $(this).find(".goodsId").data('id');
								if (oid) {
									id[id.length] = oid;
									$(".all-check,.toSettle").hide();
				                    $(".noshop").show();
								}
							}
						});
						$.ajax({
							type:'post',
							url:"{{ url('wap/cart/delete') }}",
							data:{id:id},
							success:function(data){
								if(data.success){
									location.reload();
								}
								else{
									layer.alert(data.msg);
								}
							}
						});
						$(".goods-list").remove();
						$(".all-check label").text("全选");
						$("#achecked").attr("checked",false);
						$(".all-check .img1").find("img").attr('src',"{{ url('mobile/images/no-check.png') }}");
						$(".toSettle a").addClass("nogoods");
						$(".toSettle p span i").text(0);
					}
				});
			}       
			else{
				$(".toSettle a").addClass("nogoods");
				$(".all-check label").text("全选");
				$(".goods-list").find("input[type='checkbox']").attr("checked",false);
				$(this).attr("checked",false);
				$(".img1").find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
				$(".img2").find("img").attr("src","{{ url('mobile/images/no-check.png') }}");		    
		}
		  GetCount();
        });    	
        //单条商品选中样式   	
         $(".goods-list input[type='checkbox']").unbind().click(function(){
         	var indexx2=new Array();
         	var checkStatus=($(this).attr("checked"));
         	$(".goods-list").each(function(){
         		var idNum2=$(this).find(".goodsId").attr("checked");
				if(idNum2=='checked'){
					indexx2.push($(this).index());
				}
         	});
         	if(indexx2.length==0){
         		$(".toSettle a").addClass("nogoods");
         	}
         	else{
         		$(".toSettle a").removeClass("nogoods");
         	}
         	if(checkStatus=='checked'){
         		$(this).attr("checked",true);
         		$(this).next().find("img").attr("src","{{ url('mobile/images/check.png') }}");
				$(this).next().find("img").attr("src","{{ url('mobile/images/check.png') }}");				          
         	}
         	else{
         		$("#achecked").attr("checked",false);
         		$(".all-check .img1").find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
         		$(".all-check label").text('全选');
         		$(this).attr("checked",false);
         		$(this).next().find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
				$(this).next().find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
				
         	}  
         	 GetCount();    
//       	}) 
         });
			var indexx=new Array();
			$(".all-check em").unbind().click(function(){
				$(".goods-list").each(function(){
					var idNum=$(this).find(".goodsId").attr("checked");
					if(idNum=='checked'){
						indexx.push($(this).index());						
					}
					});
					if(indexx.length==0){
						alert("请选择要删除的商品");					
					}
					else{
						var conm=confirm("确定删除商品吗？");
						if(conm==true){
							var id = new Array();
							$(".goods-list").each(function() {
								var idNum = $(this).find(".goodsId").attr("checked");
								if (idNum == 'checked') {
									var oid = $(this).find(".goodsId").data('id');
									if (oid) {
										id[id.length] = oid;
										$(".all-check,.toSettle").hide();
										$(".noshop").show();
									}
								}
							});
							$.ajax({
								type:'post',
								url:"{{ url('wap/cart/delete') }}",
								data:{id:id},
								success:function(data){
									if(data.success){
										location.reload();
									}
									else{
										layer.alert(data.msg);
									}
								}
							});
						}
				};
		    GetCount();
		});
		//去结算
		$(".qjs").click(function(){
			var goods = new Array();
			var i = 0;
			$(".goodsId").each(function(){
				if($(this).attr("checked")) {
					goods[i++] = $(this).data('id');
				}

			});
			$.post("{{ url('wap/order/submitOrder')}}",{goods:goods},function(data){
				if(data.success){
					var aClass=$(".toSettle a").attr("class");
					if(aClass=='qjs nogoods'){
						$(".toSettle a").css("cursor",'not-allowed');
						return false;
					}
					else{
						window.location.href="{{url('wap/order/commit')}}";
					}
				}else{
					layer.alert(data.msg)
					if (data.type==1) {
						window.location.href="{{url('wap/receiver/add')}}";
					}
				}
			});
		});
		//删除商品		
		$(".goods-list .good-tit em").click(function(){
			if(confirm("确定删除商品？")==true){
				var id = $(this).data('id');
				$.ajax({
					type:'post',
					url:"{{ url('wap/cart/delete') }}",
					data:{id:id},
					success:function(data){
						if(data.success){
							location.reload();
						}
						else{
							layer.alert(data.msg);
						}
					}
				});
				    if($(".goods-list").length==0){
				         $(".all-check,.toSettle").hide();
				         $(".noshop").show();
			          }
				 GetCount();
			}

		});
		 //购买数量增加减少
     	var t1=$(".buyNum");
		t1.on("change",function(){
			if(t1.val().replace(/[^0-9.]/g,'')){
			}else{
				alert("请输入正确的数值！");
				$(this).val(1);
			} 
		});
    	$(".adds").click(function(e){
    		var nowNum=parseInt($(this).parent().find(".buyNum").val())+1;
//  		addjsPrice(nowNum);
    		var t=$(this).parent().find('.buyNum');
        	$(this).parent().find(".reduces").css("cursor","pointer");
        	$(this).parent().find(".buyNum").val(parseInt(t.val())+1);
        	GetCount();
        });
        $(".reduces").click(function(){         	
        	var t=$(this).parent().find('.buyNum');
        	$(".reduces").css("cursor","pointer"); 
			$(this).parent().find(".buyNum").val(parseInt(t.val())-1);
        	if(t.val()<=1){
        		t.val(1);
        		$(this).parent().find(".reduces").css("cursor","not-allowed");    		
        	} 
        	GetCount();      	
        });
        
        
	});
	window.onload=function(){
		var numAll=$(".contt .goods-list").length;
		if(numAll==0){
			$(".toSettle a").addClass("nogoods");
		}		
		
		
	};
//商品价格
function GetCount() {
	var conts = 0;
	var aa = 0;
	$(".goods-list input[name=checkone]").each(function () {
		if ($(this).attr("checked")) {
			for (var i = 0; i < $(this).length; i++) {
				conts += parseInt($(this).parent().find('i b').text())*parseFloat($(this).parent().find('.pris em').text());
				aa += 1;
			}
		}
	});
	$(".toSettle i").html((conts).toFixed(2));
}	
</script>
</html>
